package com.insist.service.impl;

import com.insist.dao.AccountFlowDao;
import com.insist.dao.OrderDetailInfoDao;
import com.insist.dao.UserInfoDao;
import com.insist.entity.AccountFlowInfo;
import com.insist.entity.UserInfo;
import com.insist.framework.CmmBaseResponse;
import com.insist.model.AccountFlowModel;
import com.insist.service.UserService;
import com.insist.util.BeanCopierUtils;
import com.insist.util.CmmUtils;
import com.insist.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by INSIST on 2016/6/28.
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private OrderDetailInfoDao orderDetailInfoDao;
    @Autowired
    private AccountFlowDao accountFlowDao;

    @Override
    public UserInfo checkLogin(String mobile, String password) {
        return userInfoDao.getByMobileAndPassword(mobile, MD5Util.encode(password));
    }

    @Override
    public CmmBaseResponse register(String mobile, String password) {
        if(userInfoDao.getByMobile(mobile)!=null){
            return CmmBaseResponse.buildResponse(false, "用户已存在");
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setMobile(mobile);
        userInfo.setPassword(MD5Util.encode(password));
        userInfo.setUsername("用户" + CmmUtils.getHideMobile(mobile));
        userInfo.setAccount(0.0);
        userInfo.setRecMobile(mobile);
        userInfo.setRealname("用户" + CmmUtils.getHideMobile(mobile));
        userInfo = userInfoDao.save(userInfo);
        CmmBaseResponse response = CmmBaseResponse.buildResponse(true, "注册成功");
        userInfo.setPassword(null);
        response.setData(userInfo);
        return response;
    }

    @Override
    public UserInfo getByMobileAndPassword(String mobile, String password) {
        return userInfoDao.getByMobileAndPassword(mobile, password);
    }

    @Override
    public UserInfo getById(Long id) {
        return userInfoDao.getOne(id);
    }

    @Override
    public UserInfo resetPassword(String mobile) {
        UserInfo userInfo= userInfoDao.getByMobile(mobile);
        if(userInfo!=null){
            userInfo.setPassword(MD5Util.encode("12345678"));
            userInfoDao.save(userInfo);
        }
        return userInfo;
    }

    @Override
    public void save(UserInfo userInfo) {
        userInfoDao.save(userInfo);
    }

    @Override
    public Integer getTotalJoinNum() {
        return orderDetailInfoDao.countTotalJoinNum();
    }

    @Override
    public void testAddCoin(String mobile) {
        UserInfo userInfo= userInfoDao.getByMobile(mobile);
        if(userInfo!=null){
            userInfo.setAccount(userInfo.getAccount() == null ? 0 : userInfo.getAccount()+10000);
            userInfoDao.save(userInfo);
        }
    }

    @Override
    public List<AccountFlowModel> findAccountFlow(Long loginUserId) {
        List<AccountFlowModel> rsList = new ArrayList<>();
        List<AccountFlowInfo> infos = accountFlowDao.findByUserId(loginUserId);
        for(AccountFlowInfo info:infos){
            AccountFlowModel model = new AccountFlowModel();
            BeanCopierUtils.copyProperties(info, model);
            rsList.add(model);
        }
        return rsList;
    }

    @Override
    public UserInfo getByMobile(String mobile) {
        return userInfoDao.getByMobile(mobile);
    }

    @Override
    public CmmBaseResponse trideCoin(Double coin, String mobile, Long id) {
        UserInfo source = userInfoDao.getOne(id);
        if(source.getAccount()<coin){
            return CmmBaseResponse.buildResponse(false,"当前账户余额不足");
        }
        source.setAccount(source.getAccount()-coin);
        userInfoDao.save(source);

        UserInfo target = userInfoDao.getByMobile(mobile);
        target.setAccount(target.getAccount()+coin);
        userInfoDao.save(target);

        //消费流水
        AccountFlowInfo accountFlowInfo = new AccountFlowInfo();
        accountFlowInfo.setBizId(0l);
        accountFlowInfo.setUserId(source.getId());
        accountFlowInfo.setCreateTime(new Date());
        accountFlowInfo.setMessage("账户转移到"+target.getMobile()+"消费");
        accountFlowInfo.setType(2);
        accountFlowInfo.setMoney(coin);
        accountFlowDao.save(accountFlowInfo);

        //收入流水
        AccountFlowInfo accountFlowInfo2 = new AccountFlowInfo();
        accountFlowInfo2.setBizId(0l);
        accountFlowInfo2.setUserId(target.getId());
        accountFlowInfo2.setCreateTime(new Date());
        accountFlowInfo2.setMessage("系统赠送");
        accountFlowInfo2.setType(1);
        accountFlowInfo2.setMoney(coin);
        accountFlowDao.save(accountFlowInfo2);

        return CmmBaseResponse.buildResponse(true,"转移成功");
    }

}
